﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OpenExpressApp
{
    /// <summary>
    /// Interaction logic for ConfigWindow.xaml
    /// </summary>
    public partial class ConfigWindow : Window
    {
        public ConfigWindow()
        {
            InitializeComponent();

            this.DisplayConfig();
        }

        private bool Modified
        {
            get
            {
                return this.Title.EndsWith("*");
            }
            set
            {
                this.Title = "配置窗口";
                if (value)
                {
                    this.Title += '*';
                }
            }
        }

        private void BSave_Click(object sender, RoutedEventArgs e)
        {
            this.SaveConfig();
        }

        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            this.Modified = true;
        }

        private void DisplayConfig()
        {
            var config = AppConfig.Instance;

            txtDB_Server.Text = config.DB_Server;
            txtDB_Password.Text = config.DB_Password;
            txtDB_UserID.Text = config.DB_UserID;
            txtWCFPort.Text = config.WCFPort.ToString();
            txtDBName_BQNorm.Text = config.DBName_BQNorm;
            txtDBName_Estimate.Text = config.DBName_Estimate;
            txtDBName_GIX4.Text = config.DBName_GIX4;
            txtDBName_OpenExpressApp.Text = config.DBName_OEA;

            this.Modified = false;
        }

        private void SaveConfig()
        {
            #region Check input

            if (this.Modified == false)
            {
                this.DialogResult = false;
                return;
            }

            int port = 0;
            if (int.TryParse(txtWCFPort.Text, out port) == false)
            {
                MessageBox.Show("端口后必须是整数。");
                return;
            } 

            #endregion

            var config = AppConfig.Instance;

            config.DB_Server = txtDB_Server.Text;
            config.DB_Password = txtDB_Password.Text;
            config.DB_UserID = txtDB_UserID.Text;
            config.WCFPort = port;
            config.DBName_BQNorm = txtDBName_BQNorm.Text;
            config.DBName_Estimate = txtDBName_Estimate.Text;
            config.DBName_GIX4 = txtDBName_GIX4.Text;
            config.DBName_OEA = txtDBName_OpenExpressApp.Text;

            if (config.Save())
            {
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
        }
    }
}
